﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lswweb.IDAL;
using Lswweb.Model;

namespace Lswweb.Domain
{
    public class SinglePages
    {
        private static ISinglePages singlePageDAL = DALFactory.CreateSinglePagesDAL();

        public static SinglePageInfo GetSinglePage(SinglePage singlePage)
        {
            return singlePageDAL.GetSinglePage(singlePage,SiteContext.Current.Language);
        }

        public static void Update(SinglePageInfo info)
        {
            if (info == null || !Enum.IsDefined(typeof(SinglePage),info.Id))
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            SinglePageInfo oldInfo = GetSinglePage(info.Id);

            if (!string.IsNullOrEmpty(info.ImageUrl))
            {
                if (oldInfo != null && !string.IsNullOrEmpty(oldInfo.ImageUrl))
                    Globals.FileDelete(oldInfo.ImageUrl);

                info.ImageUrl = ImageHelper.MoveImagesToDirectory(SiteModule.SinglePage, info.ImageUrl);
            }
            else
            {
                if(oldInfo!=null)
                    info.ImageUrl = oldInfo.ImageUrl;
            }

            info.Ver = SiteContext.Current.Language;

            if (singlePageDAL.Update(info))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }
    }
}
